// https://leetcode.cn/problems/two-sum/

package main

import (
	"fmt"
)

func findMedianSortedArrays(nums1 []int, nums2 []int) float64 {

	l1, l2, cur1, cur2, sum_cur, median_idx, is_double, l1_end, l2_end := len(nums1), len(nums2), 0, 0, 0, 0, false, false, false
	sum_l := l1 + l2
	if sum_l%2 == 0 {
		median_idx = (sum_l / 2) - 1
		is_double = true
	} else {
		median_idx = sum_l / 2
	}

	first_median, _first := 0, true

	for {
		v := 0
		l1_end, l2_end = cur1 == l1, cur2 == l2
		if !l1_end && !l2_end {
			v1, v2 := nums1[cur1], nums2[cur2]
			if v1 < v2 {
				v = v1
				cur1 += 1
			} else {
				v = v2
				cur2 += 1
			}
		} else if l1_end {
			v = nums2[cur2]
			cur2 += 1
		} else if l2_end {
			v = nums1[cur1]
			cur1 += 1
		}

		if sum_cur < median_idx {
			sum_cur += 1
			continue
		}

		if is_double {
			if _first {
				first_median, _first = v, false
				continue
			}
			return float64(first_median+v) / 2
		} else {
			return float64(v)
		}
	}
}

func main() {
	// [1,3], nums2 = [2]
	nums1, nums2 := []int{1, 3}, []int{2}
	res := findMedianSortedArrays(nums1, nums2)
	fmt.Printf("%v\n", res)
	// 2 abba
}
